﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SCRUM.Core.DAO;
using SCRUM.Core.BusinessObjects;
using SCRUM.Core.MembershipProviders;

/// <summary>
/// Represents selection user control for hierarchy from Project to backlog item 
/// </summary>
public partial class BliSelection : HierachicalObjectSelection
{
    /// <summary>
    /// Event that is triggered when bli changes
    /// </summary>
    public event EventHandler SelectedBliChangedEvent;

    /// <summary>
    /// Event triggered when user story changes
    /// </summary>
    public event EventHandler SelectedUserStoryChangedEvent;

    /// <summary>
    /// Event triggered when sprint changes
    /// </summary>
    public event EventHandler SelectedSprintChangedEvent;

    /// <summary>
    /// Event triggered when release changes
    /// </summary>
    public event EventHandler SelectedReleaseChangedEvent;

    /// <summary>
    /// Event triggered when project changes
    /// </summary>
    public event EventHandler SelectedProjectChangedEvent;

    /// <summary>
    /// Gets the selected Bli.
    /// </summary>
    /// <value>The selected Bli.</value>
    public BLI SelectedBli
    {
        get
        {
            if (String.IsNullOrEmpty(this.DropDownListBlis.SelectedValue) || this.DropDownListBlis.SelectedValue == "-1")
            {
                return null;
            }
            else
            {
                return DAOBli.GetBliByID(Int32.Parse(this.DropDownListBlis.SelectedValue));
            }
        }
    }

    /// <summary>
    /// Gets the selected bli ID.
    /// </summary>
    /// <value>The selected bli ID.</value>
    public int SelectedBliID
    {
        get
        {
            if (String.IsNullOrEmpty(this.DropDownListBlis.SelectedValue) || this.DropDownListBlis.SelectedValue == "-1")
            {
                return -1;
            }
            else
            {
                return Int32.Parse(this.DropDownListBlis.SelectedValue);
            }
        }
    }

    /// <summary>
    /// REpresents selected user story id
    /// </summary>
    public int SelectedUserStoryID
    {
        get
        {
            return UserStorySelectionBli.SelectedUserStoryID;
        }
    }

    /// <summary>
    /// represents selected sprint id
    /// </summary>
    public int SelectedSprintID
    {
        get
        {
            return UserStorySelectionBli. SelectedSprintID;
        }
    }

    /// <summary>
    /// Represents selected release id
    /// </summary>
    public int SelectedReleaseID
    {
        get
        {
            return UserStorySelectionBli.SelectedReleaseID;
        }
    }

    /// <summary>
    /// Represents seleted project id
    /// </summary>
    public int SelectedProjectID
    {
        get
        {
            return UserStorySelectionBli.SelectedProjectID;
        }
    }

    /// <summary>
    /// Sets handlers for events.
    /// </summary>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected override void OnInit(EventArgs e)
    {
        this.UserStorySelectionBli.SelectedUserStoryChangedEvent += new EventHandler(SelectedUserStoryChanged);
        this.UserStorySelectionBli.SelectedSprintChangedEvent += new EventHandler(SelectedSprintChanged);
        this.UserStorySelectionBli.SelectedReleaseChangedEvent += new EventHandler(SelectedReleaseChanged);
        this.UserStorySelectionBli.SelectedProjectChangedEvent += new EventHandler(SelectedProjectChanged);

        this.UserStorySelectionBli.SelectedUserStoryChangedEvent += new EventHandler(SelectedUserStoryChangedKeeper);
        this.UserStorySelectionBli.SelectedSprintChangedEvent += new EventHandler(SelectedSprintChangedKeeper);
        this.UserStorySelectionBli.SelectedReleaseChangedEvent += new EventHandler(SelectedReleaseChangedKeeper);
        this.UserStorySelectionBli.SelectedProjectChangedEvent += new EventHandler(SelectedProjectChangedKeeper);
    }

    /// <summary>
    /// It keeps projectselectionevent for the other selections.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void SelectedProjectChangedKeeper(object sender, EventArgs e)
    {
        if (this.IsPostBack)
        {
            if (this.SelectedProjectChangedEvent != null)
            {
                this.SelectedProjectChangedEvent(sender, e);
            }
        }
    }

    /// <summary>
    /// It keeps releaseselectionevent for the other selections.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void SelectedReleaseChangedKeeper(object sender, EventArgs e)
    {
        if (this.IsPostBack)
        {
            if (this.SelectedReleaseChangedEvent != null)
            {
                this.SelectedReleaseChangedEvent(sender, e);
            }
        }
    }

    /// <summary>
    /// It keeps sprintselectionevent for the other selections.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void SelectedSprintChangedKeeper(object sender, EventArgs e)
    {
        if (this.IsPostBack)
        {
            if (this.SelectedSprintChangedEvent != null)
            {
                this.SelectedSprintChangedEvent(sender, e);
            }
        }
    }

    /// <summary>
    /// It keeps sprintselectionevent for the other selections.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void SelectedUserStoryChangedKeeper(object sender, EventArgs e)
    {
        if (this.IsPostBack)
        {
            if (this.SelectedUserStoryChangedEvent != null)
            {
                this.SelectedUserStoryChangedEvent(sender, e);
            }
        }
    }

    /// <summary>
    /// Handles the Load event of the Page control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            DataBind();
        }
    }

    /// <summary>
    /// Loads the Blis.
    /// </summary>
    protected void LoadBlis()
    {
        UserStory u = UserStorySelectionBli.SelectedUserStory;
        if (u == null)
        {
            Sprint s = UserStorySelectionBli.GetSelectedSprint();
            if (s == null)
            {
                Release r = UserStorySelectionBli.GetSelectedRelease();
                if (r == null)
                {
                    Project p = UserStorySelectionBli.GetSelectedProject();
                    if (p == null)
                    {
                        DropDownListBlis.DataSource = DAOBli.GetAllBlis();
                    }
                    else
                    {
                        DropDownListBlis.DataSource = DAOBli.GetAllBlisForProject(p.ID);
                    }
                }
                else
                {
                    DropDownListBlis.DataSource = DAOBli.GetAllBlisForRelease(r.ID);
                }
            }
            else
            {
                DropDownListBlis.DataSource = DAOBli.GetAllBlisForSprint(s.ID);
            }
        }
        else
        {
            DropDownListBlis.DataSource = DAOBli.GetAllBlisForUserStory(u.ID);
        }

        DropDownListBlis.DataTextField = "Name";
        DropDownListBlis.DataValueField = "ID";
        DropDownListBlis.DataBind();
        DropDownListBlis.Items.Insert(0, TopListItemForFilter("Filters","AnyBli") );
    }

    /// <summary>
    /// Binds a data source to the invoked server control and all its child controls.
    /// </summary>
    public override void DataBind()
    {
        base.DataBind();
        UserStorySelectionBli.DataBind();
        LoadBlis();
    }

    /// <summary>
    /// Selected user story changed.
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void SelectedUserStoryChanged(object sender, EventArgs e)
    {
        if (this.IsPostBack)
        {
            LoadBlis();
        }
    }

    /// <summary>
    /// Selected sprint changed.
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void SelectedSprintChanged(object sender, EventArgs e)
    {
        if (this.IsPostBack)
        {
            LoadBlis();
        }
    }

    /// <summary>
    /// Selected release changed.
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void SelectedReleaseChanged(object sender, EventArgs e)
    {
        if (this.IsPostBack)
        {
            LoadBlis();
        }
    }

    /// <summary>
    /// Selected project changed.
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void SelectedProjectChanged(object sender, EventArgs e)
    {
        if (this.IsPostBack)
        {
            LoadBlis();
        }
    }

    /// <summary>
    /// Selected Bli changed.
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void SelectedBliChanged(object sender, EventArgs e)
    {
        if (this.IsPostBack)
        {
            if (this.SelectedBliChangedEvent != null)
            {
                this.SelectedBliChangedEvent(sender, e);
            }

            if (this.SelectedBli == null)
            {
                return;
            }

            int selectedBliID = this.SelectedBli.ID;
            UserStorySelectionBli.SetProjectReleaseSprintUserStoryByBli(selectedBliID);

            // Reload Blis
            LoadBlis();

            // Select the right Bli
            for (int i = 0; i < this.DropDownListBlis.Items.Count; i++)
            {
                if (this.DropDownListBlis.Items[i].Value == selectedBliID.ToString())
                {
                    this.DropDownListBlis.SelectedIndex = i;
                    break;
                }
            }
        }
    }

    /// <summary>
    /// Clears the dropdown lists.
    /// </summary>
    public void Clear(object sender, EventArgs e)
    {
        this.UserStorySelectionBli.Clear(sender, e);
    }

    protected override WebControl GetWebControl()
    {
        return this.DropDownListBlis;
    }

    protected override Label GetLabel()
    {
        return this.LabelBlis;
    }
}
